<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button type="dashed" @click="addAccount()" icon="plus" block>新增</a-button>
      <!--<a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>-->
    </div>

    <!-- table区域-begin -->
    <div>
      <!--<div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>-->

      <div class="list">
        <van-list
          v-model="loading"
          :finished="finished"
          finished-text="没有更多了"
          @load="onLoadList"
        >
          <div
            class="customer-list"
            v-for="(item,i) in dataSource"
            @click="accountDetail(item)"
            :key="i">
            <div>
              <div class="name">{{item.accountName || ''}}</div>
              <div class="contact">
                <span>账户类型：{{ getAccountType(item.accountType) || '无' }}</span>
                <span style="float: right">
                  <a @click.stop="editAccount(item)">编辑</a>
                  <a-divider type="vertical" />
                  <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(item.id)">
                    <a @click.stop="">删除</a>
                  </a-popconfirm>
                </span>
              </div>
              <div class="contact">
                <span>是否启用：<a-checkbox :checked="item.isStartUsing == 1" @click.stop="changeIsStartUsing(item)" ></a-checkbox></span>
              </div>
              <div class="contact">
                <span>账户余额：{{ item.surplus || '无'}}</span>
              </div>
            </div>
          </div>
        </van-list>
      </div>

    </div>

    <add-account-modal ref="addAccountModal" @ok="modalFormOk"></add-account-modal>
    <account-detail-modal ref="accountDetailModal"></account-detail-modal>
  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import addAccountModal from "./modules/addAccountModal";
  import accountDetailModal from "./modules/accountDetailModal";
  import {deleteAction, getAction, putAction} from "../../../../api/manage";

  export default {
    name: 'financeAccountList',
    mixins:[JeecgListMixin, mixinDevice],
    components: {
      addAccountModal,accountDetailModal,
    },
    data () {
      return {
        description: '财务账户管理管理页面',
        dataSource:[],
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'账户名称',
            align:"center",
            dataIndex: 'accountName',
            slots: { title: 'customTitle' },
            scopedSlots: { customRender: 'accountName' },
          },
          {
            title:'账户类型',
            align:"center",
            dataIndex: 'accountType',
            customRender:function (text) {
              switch (text) {
                case 1:
                  return "银行"
                  break;
                case 2:
                  return "现金"
                  break;
                case 3:
                  return "第三方"
                  break;
              }
            }
          },
          {
            title:'会计科目',
            align:"center",
            dataIndex: 'accountingSubject'
          },
          {
            title:'银行信息',
            align:"center",
            dataIndex: 'bankInfo'
          },
          {
            title:'账户余额',
            align:"center",
            dataIndex: 'surplus'
          },
          {
            title:'流入',
            align:"center",
            dataIndex: 'inflow'
          },
          {
            title:'流出',
            align:"center",
            dataIndex: 'outflow'
          },
          {
            title:'启用',
            align:"center",
            dataIndex: 'isStartUsing',
            slots: { title: 'customTitle' },
            scopedSlots: { customRender: 'isStartUsing' },
          },
          {
            title:'备注',
            align:"center",
            dataIndex: 'remark'
          },

          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/ghFinanceAccount/list",
          delete: "/ghFinanceAccount/delete",
          deleteBatch: "/ghFinanceAccount/deleteBatch",
          exportXlsUrl: "/ghFinanceAccount/exportXls",
          importExcelUrl: "/ghFinanceAccount/importExcel",
          edit: "/ghFinanceAccount/edit",
        },
        dictOptions:{},
        superFieldList:[],
        finished: false,
      }
    },
    created() {

    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      addAccount(){
        this.$refs.addAccountModal.title = "新增账户";
        this.$refs.addAccountModal.add();
      },
      editAccount(record){
        this.$refs.addAccountModal.title = "编辑账户";
        this.$refs.addAccountModal.edit(record);
      },
      modalFormOk(){
        this.loadData();
      },
      accountDetail(record){
        this.$refs.accountDetailModal.showDrawer(record);
        this.$refs.accountDetailModal.title = record.accountName;
      },

      changeIsStartUsing(record){
        let params = {};
        params.id = record.id;
        if (record.isStartUsing == 1){
          params.isStartUsing = 2;
        }else if (record.isStartUsing == 2){
          params.isStartUsing = 1;
        }
        putAction(this.url.edit,params).then((res)=> {
          if (res.success) {
            this.$message.success(res.message);
            this.loadData(1);
          } else {
            this.$message.warning(res.message);
          }
        })
      },
      //下一页
      onLoadList(){
        this.ipagination.current++
        this.loadData(this.ipagination.current)
      },
      loadData(arg){
        //加载数据 若传入参数1则加载第一页的内容
        if (arg === 1) {
          this.ipagination.current = 1;
          this.dataSource = [];
          this.finished = false;
        }
        let params = this.getQueryParams();//查询条件
        this.loading = true
        getAction(this.url.list, params).then((res) => {
          if (res.success) {
            this.dataSource = [...this.dataSource, ...res.result.records]
            if (res.result.total) {
              this.ipagination.total = res.result.total
            } else {
              this.ipagination.total = 0
            }
          } else {
            this.$message.warning(res.message)
          }
          this.loading = false
          if(res.result.records.length<10){
            this.finished = true;
          }
        }).finally(() => {
          this.loading = false
        })
      },
      handleDelete(id) {
        if(!this.url.delete){
          this.$message.error("请设置url.delete属性!")
          return
        }
        let that = this;
        that.loading = true;
        deleteAction(that.url.delete, {id: id}).then((res) => {
          if (res.success) {
            //重新计算分页问题
            that.$message.success(res.message);
            that.loadData(1);
          } else {
            that.$message.warning(res.message);
          }
        }).finally(() => {
          this.loading = false
        });
      },
      getAccountType(value){
        switch (value) {
          case 1:
            return "银行"
            break;
          case 2:
            return "现金"
            break;
          case 3:
            return "第三方"
            break;
        }
      },
    }
  }
</script>
<style scoped lang="less">
  @import '~@assets/less/common.less';
  .select-pk{
    display:flex;
    align-items:center;
    margin:10px 0;
    &>span{
      font-weight: bold;
    }
    &-text{
      width: calc(100% - 6em);
      height: 30px;
      text-indent: 1em;
      line-height: 28px;
      border: 1px solid #d9d9d9;
      border-radius: 4px;
    }
  }
  .list{
    overflow-y: scroll;
    height:100vh;
    margin-top: 10px;
  }
  ::-webkit-scrollbar {
    display: none;
  }
  .customer-list{
    /*display: flex;*/
    align-items: center;
    border-top: 1px solid #d9d9d9;
    .name{
      font-size: 13px;
      color: #161616;
      line-height: normal;
      margin-top: 5px;
    }
    .contact{
      margin-top: 5px;
      font-size: 12px;
      color: #a39f9f;
      margin-bottom: 5px;
    }
  }
</style>